package com.example.practicetest.dao;

import com.example.practicetest.entity.User;

import java.sql.*;

/**
 * @author wushuang
 * User的dao层
 * 用户user的增删查改
 */
public class UserDao {
    private String url = "jdbc:mysql://localhost:3306/test";
    private String username = "root";
    private String password = "root";
    private String driver = "com.mysql.cj.jdbc.Driver";
    private Connection connection = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    /**
     * 添加用户
     * @param user
     * @return
     * @throws SQLException
     */
    public int addUser(User user){
        // 记录更改的行数
        int count = 0;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "insert into tb_user(username,password) values(?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            count = ps.executeUpdate();
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return count;
        }
    }

    /**
     * 删除用户
     * @param uname
     * @return
     * @throws SQLException
     */
    public int deleteUser(String uname){
        int count = 0;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "delete from tb_user where username = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,uname);
            count = ps.executeUpdate();
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return count;
        }
    }

    /**
     * 更新用户
     * @param user
     * @return
     * @throws SQLException
     */
    public int updateUser(User user){
        int count = 0;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "update tb_user set password = ? where username = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getUsername());
            count = ps.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            releaseAll();
            return count;
        }
    }

    /**
     * 查询用户
     * @param uname
     * @return
     * @throws SQLException
     */
    public User findUser(String uname){
        User user = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "select * from tb_user where username = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,uname);
            rs = ps.executeQuery();
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return user;
        }
    }

    /**
     * 释放资源
     */
    public void releaseAll(){
        try{
            if (ps != null){
                ps.close();
            }
            if (rs != null){
                rs.close();
            }
            if (connection != null){
                connection.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

}
